package com.zhentao.utils;


import com.alibaba.fastjson.JSONObject;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class AnalysisRealName {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static Map<String, Object> analysisRealName(String json) {
        Map<String, Object> map = new HashMap<>();
        try {
            JSONObject parse = JSONObject.parseObject(json);
            if (!ObjectUtils.isEmpty(parse)) {
                Integer errorCode = parse.getInteger("error_code");
                JSONObject result = parse.getJSONObject("result");
                Boolean isok = result.getBoolean("isok");

                if (errorCode != 0 || !isok) {
                    map.put("isok", isok);
                    map.put("reason", parse.getString("reason"));
                } else {
                    JSONObject idCardInfor = result.getJSONObject("IdCardInfor");
                    if (idCardInfor != null) {
                        String birthday = idCardInfor.getString("birthday");
                        map.put("isok", isok);
                        map.put("gender", "男".equals(idCardInfor.getString("sex")) ? 0 : 1);
                        map.put("birthdate", sdf.parse(birthday));
                        map.put("nativePlace", idCardInfor.getString("area"));
                    } else {
                        // Handle the case where IdCardInfor is null
                        map.put("isok", false);
                        map.put("reason", "身份信息缺失");
                    }
                }
            }
        } catch (ParseException e) {
            // Handle the ParseException
            map.put("isok", false);
            map.put("reason", "日期解析错误: " + e.getMessage());
        }
        return map;
    }
}
